package com.tianqu.tms.www.transfer.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.WeChatMqApi;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn000437Bo;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.TransferProcessHistoryDaoImp;
import com.tianqu.tms.dto.TransferProcessHistoryDto;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.CarOwnerAuditActionForm;

public class CarOwnerAuditResultController extends TmsController {
	private Logger logger = Logger.getLogger(CarOwnerAuditResultController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = getTxController();
		try {
			controller.begin();
			TransferAcceptedInfo acceptedInfo = TransferAcceptedInfo.getInstance();
			TmsUserProfile profile = uc.getUserProfile();
			CarOwnerAuditActionForm inputForm = (CarOwnerAuditActionForm)uc.getInputForm();
			int transferType = inputForm.getTransferType();
			String auditFlg = inputForm.getAuditFlg();
			String serial = inputForm.getSerial();
			acceptedInfo.setSerial(serial);
			
			if ("1".equals(auditFlg)) {
				
				// 更新 过户业务基本信息表 (tms_transfer_accepted_info)
				acceptedInfo.setTransfer(3);// 车主审核中
				acceptedInfo.setUpdateUserId(profile.getUniqueId());// 存入更新者Id
				acceptedInfo.updateCarOwnerAuditBySerial(acceptedInfo);

			} else if ("2".equals(auditFlg)) {
				
				// 更新 过户业务基本信息表 (tms_transfer_accepted_info)
				acceptedInfo.setTransfer(1);// 车主审核通过
				acceptedInfo.setUpdateUserId(profile.getUniqueId());// 存入更新者Id
				if (transferType == 5) {
					acceptedInfo.setStatus(10); // 审档中
				}
				
				acceptedInfo.updateCarOwnerAuditBySerial(acceptedInfo);
			}

			// 插入 过户流程操作记录表（tms_transfer_process_history）
			TransferProcessHistoryDto transferProcessHistoryDto = new TransferProcessHistoryDto();
			transferProcessHistoryDto.setBusinessType(48); // 车主审核
			transferProcessHistoryDto.setInsertUserId(profile.getUniqueId());
			transferProcessHistoryDto.setSerial(inputForm.getSerial());
			TransferProcessHistoryDaoImp transferProcessHistoryDao = (TransferProcessHistoryDaoImp) TransferProcessHistoryDaoImp.getInstance(TmsDao.TMS_TRANSFER_PROCESS_HISTORY);
			transferProcessHistoryDao.insertHisInfoBySerial(transferProcessHistoryDto);
			
			controller.commit();
	
			//437
			TransferDataSyn000437Bo transferDataSyn000437Bo = new TransferDataSyn000437Bo();
			transferDataSyn000437Bo.setMarketNo(TmsConstants.MARKET_NO);
			transferDataSyn000437Bo.setRemark("");
			transferDataSyn000437Bo.setSerial(inputForm.getSerial());
			transferDataSyn000437Bo.setStatus(acceptedInfo.getTransfer());
			WeChatMqApi.getInstance().call(transferDataSyn000437Bo);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");

	}
}
